package com.travel.server.manager.impl.command.order.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.data.QueryOrderDataRequest;
import com.travel.server.manager.dto.response.order.data.QueryOrderDataResponse;
import com.travel.server.manager.dto.response.order.data.QueryOrderDataResponse.PayDataItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import com.travel.server.manager.model.dto.data.OrderDataDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/7 16:20
 * @Desc:
 */
@Component
@Slf4j
public class QueryOrderDataCommand extends AbstractCommand<QueryOrderDataRequest, QueryOrderDataResponse> {

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(QueryOrderDataRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryOrderDataResponse handle(QueryOrderDataRequest request) {
        checkInput(request);
        DataDateVo dateVo = OrderDataSal.getDate(request.getType(), request);
        Date startTime = dateVo.getStartTime();
        Date endTime = dateVo.getEndTime();
        Date lastTime = dateVo.getLastTime();

        List<OrderDataDto> list = orderDal.selectOrderData(lastTime, endTime);
        return makeResponse(list, startTime, endTime);
    }

    private QueryOrderDataResponse makeResponse(List<OrderDataDto> list, Date startTime, Date endTime){
        QueryOrderDataResponse response = new QueryOrderDataResponse();
        int currentOrderNum = 0;
        int currentPayNum = 0;
        int currentPayAmount = 0;
        int currentApplyNum = 0;
        int currentPayAllNum = 0;
        int lastOrderNum = 0;
        int lastPayNum = 0;
        int lastPayAmount = 0;
        int lastApplyNum = 0;
        int lastPayAllNum = 0;
        if(!CollectionUtil.isEmpty(list)){
            for(OrderDataDto dataDto : list){
                Date date = DateUtil.getDataTime(dataDto.getDays());
                if(date.before(startTime)){
                    lastOrderNum += dataDto.getOrderNum();
                    lastPayNum += dataDto.getPayNum();
                    lastPayAmount += dataDto.getPayAmount();
                    lastApplyNum += dataDto.getApplyNum();
                    lastPayAllNum += dataDto.getPayAllNum();
                }else{
                    currentPayAllNum += dataDto.getPayAllNum();
                    currentOrderNum += dataDto.getOrderNum();
                    currentPayNum += dataDto.getPayNum();
                    currentPayAmount += dataDto.getPayAmount();
                    currentApplyNum += dataDto.getApplyNum();
                }
            }
        }
        response.setOrder(currentOrderNum + "");
        response.setPayAmount(CommonUtil.fenToYuan(currentPayAmount).toString());
        response.setPayNum(currentPayNum + "");
        response.setApplyNum(currentApplyNum + "");
        response.setPayAllNum(currentPayAllNum + "");
        response.setOrderCompare(OrderDataSal.getProportion(currentOrderNum, lastOrderNum));
        response.setPayNumCompare(OrderDataSal.getProportion(currentPayNum, lastPayNum));
        response.setPayAmountCompare(OrderDataSal.getProportion(currentPayAmount, lastPayAmount));
        response.setApplyNumCompare(OrderDataSal.getProportion(currentApplyNum, lastApplyNum));
        response.setPayAllNumCompare(OrderDataSal.getProportion(currentPayAllNum, lastPayAllNum));
        response.setVisit("0");
        response.setVisitCompare("0%");
        response.setVisitOrder("0%");
        response.setVisitCompare("0%");
        response.setVisitPay("0%");
        response.setProjectVisitApply("0%");
        response.setOrderPay(CommonUtil.getProportion(currentOrderNum, currentPayNum));
        response.setApplyPayAll(CommonUtil.getProportion(currentApplyNum, currentPayAllNum));
        response.setPayDataList(makePayDataList(list, startTime, endTime));

        return  response;
    }

    private List<PayDataItem> makePayDataList(List<OrderDataDto> list, Date startTime, Date endTime){
        List<PayDataItem> payDataList = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)){
            PayDataItem item = new PayDataItem();
            item.setDate(calendar.getTime());
            item.setPayAmount("0");
            item.setPayNum("0");
            if(!CollectionUtil.isEmpty(list)) {
                int payNum = 0;
                int payAmount = 0;
                for (OrderDataDto orderDataDto : list) {
                    String day = DateUtil.YYYY_MM_DD.format(calendar.getTime());
                    if (day.equals(orderDataDto.getDays())) {
                        payNum += orderDataDto.getPayNum();
                        payAmount += orderDataDto.getPayAmount();
                    }
                }
                item.setPayNum(payNum + "");
                item.setPayAmount(CommonUtil.fenToYuan(payAmount).toString());
            }
            payDataList.add(item);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        return payDataList;
    }
}
